<?php
class b2c_ctl_site_goodscatApi {
	public function index(){
		header('Content-Type:text/html; charset=utf-8');
		// var_dump($_POST) ;
		// exit();
        $EData = $_POST['EData']; 
        if(!$EData)
		{
		   echo $this->Result('false',"Edata参数不能为空",$_POST);
           exit();  
		}
 // $EData='[{"cat_id":"150","parent_id":"0","is_leaf":"false","cat_name":"日用品a","disabled":"false","child_count":0,"type_id":1},{"cat_id":"160","parent_id":"150","is_leaf":"false","cat_name":"电器b","disabled":"false","child_count":0,"type_id":1}]';
     //   $sale_arr = array();
     //    $sale_arr[] =array(
     //    'cat_id' => '119',
     //    'parent_id' => '0',
     //    'cat_name' => '119',
     //    'child_count' => 0,
     //    'type_id'=> 1,
     //    );

	    // $sale_arr[] =array(
     //    'cat_id' => '120',
     //    'parent_id' => '0',
     //    'cat_name' => '120',
     //    'child_count' => 0,
     //    'type_id'=> 1,
     //    );
     $sale_arr =  json_decode( $EData,true);
     // echo $sale_arr;
 
 
        if(!empty($sale_arr)){
            foreach($sale_arr as $val){
   
            	$objCat = app::get("b2c")->model('goods_cat');

               try {
                     $res = $objCat->save($val);//insert
               }
				catch (Exception $e) {
				    echo $this->Result('false',$e->getMessage(),$_POST); 
				    exit();
			   }

                if(empty($res))
                {
                   $msg .=  '【'. $val['cat_id'].'】' ;
                }else
                {
                	//更新：商品分类设置 默认值
                	$gallery_setting='a:1:{s:16:"gallery_template";s:1:"0";}';
                	$sql="update sdb_b2c_goods_cat set gallery_setting='".$gallery_setting."' where cat_id=".$val['cat_id'];
                   logger::info(' 同步SQL：'. $sql);
                   $orderres = $objCat->db->exec($sql);
                }
             
            }
         }else
         {
         	 echo $this->Result('false','json 数据格式错误',$_POST);
         	 exit();
         }
         // echo '<pre>';
         // echo $this->json_encode_ex($sale_arr);
		 if(empty($msg))
		 {
		 	 echo $this->Result('true','',$_POST);
		 }else
		 {
             echo $this->Result('false','分类ID：'.$msg.'同步失败',$_POST);
		 }
		
         exit();

		file_put_contents("order_api_import.txt", var_export($response_datas, 1));
	}

    //封装成json
	public function json_encode_ex($array) {
		if (version_compare(PHP_VERSION,'5.4.0','<')) {
			$str = json_encode($array);
			$str = preg_replace_callback (
				"#\\\u([0-9a-f]{4})#i", 
				function($matchs) {
					return iconv('UCS-2BE', 'UTF-8',  pack('H4',  $matchs[1])); 
				},
				$str
				);
			return $str;
		} else {
			return json_encode($array, JSON_UNESCAPED_UNICODE);
		}
	}
		   //
		/**
	* 解析json串
	*  type $json_str
	*  type
	*/
	function analyJson($json_str) {
		$json_str = str_replace('＼＼', '', $json_str);
		$out_arr = array();
		preg_match('/{.*}/', $json_str, $out_arr);
		if (!empty($out_arr)) {
		$result = json_decode($out_arr[0], TRUE);
		} else {
		return FALSE;
		}
		return $result;
	}


	public function  Result($successs,$msg,$str)
	{
		$sdf= array(
			'successs'   =>$successs,
			'msg'   => $msg ,
            'str' => $str
			);
		return $this->json_encode_ex($sdf);

	}
}